package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.MigrateFieldMapping;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移字段映射信息类-[表名: 迁移字段映射信息(migrate_field_mapping)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateFieldMappingDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateFieldMappingDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateFieldMapping(MigrateFieldMapping migrateFieldMapping) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_field_mapping ( "
                + "field_mapping_id,table_mapping_id,source_field_code,source_field_name,field_mapping_rule,target_field_code,target_field_name"
                + ") values ("+ "?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateFieldMapping.getFieldMappingId());
            ps.setObject(2, migrateFieldMapping.getTableMappingId());
            ps.setObject(3, migrateFieldMapping.getSourceFieldCode());
            ps.setObject(4, migrateFieldMapping.getSourceFieldName());
            ps.setObject(5, migrateFieldMapping.getFieldMappingRule());
            ps.setObject(6, migrateFieldMapping.getTargetFieldCode());
            ps.setObject(7, migrateFieldMapping.getTargetFieldName());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateFieldMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateFieldMapping migrateFieldMapping) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_field_mapping where 1=1"
                + " AND field_mapping_id = ? "
                );
            ps.setObject(1, migrateFieldMapping.getFieldMappingId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateFieldMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateFieldMapping migrateFieldMapping) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_field_mapping set "
                + " field_mapping_id = ? ,"
                + " table_mapping_id = ? ,"
                + " source_field_code = ? ,"
                + " source_field_name = ? ,"
                + " field_mapping_rule = ? ,"
                + " target_field_code = ? ,"
                + " target_field_name = ? "
                + " where 1=1"
                + " AND field_mapping_id = ? "
                );
            ps.setObject(1, migrateFieldMapping.getFieldMappingId());
            ps.setObject(2, migrateFieldMapping.getTableMappingId());
            ps.setObject(3, migrateFieldMapping.getSourceFieldCode());
            ps.setObject(4, migrateFieldMapping.getSourceFieldName());
            ps.setObject(5, migrateFieldMapping.getFieldMappingRule());
            ps.setObject(6, migrateFieldMapping.getTargetFieldCode());
            ps.setObject(7, migrateFieldMapping.getTargetFieldName());
            ps.setObject(8, migrateFieldMapping.getFieldMappingId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateFieldMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateFieldMapping queryByPk(MigrateFieldMapping migrateFieldMapping) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateFieldMapping result = null;
        try {
            ps = conn.prepareStatement("select field_mapping_id,table_mapping_id,source_field_code,source_field_name,field_mapping_rule,target_field_code,target_field_namefrom migrate_field_mapping where 1=1 "
                + " AND field_mapping_id = ? "
                );
            ps.setObject(1, migrateFieldMapping.getFieldMappingId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateFieldMapping();
				result.setFieldMappingId(rs.getString("field_mapping_id"));
				result.setTableMappingId(rs.getString("table_mapping_id"));
				result.setSourceFieldCode(rs.getString("source_field_code"));
				result.setSourceFieldName(rs.getString("source_field_name"));
				result.setFieldMappingRule(rs.getString("field_mapping_rule"));
				result.setTargetFieldCode(rs.getString("target_field_code"));
				result.setTargetFieldName(rs.getString("target_field_name"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateFieldMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateFieldMapping 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateFieldMapping> queryAll(MigrateFieldMapping migrateFieldMapping) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateFieldMapping> result = new ArrayList<>();
        try {
            String sql = "select field_mapping_id,table_mapping_id,source_field_code,source_field_name,field_mapping_rule,target_field_code,target_field_namefrom migrate_field_mapping where 1=1 ";
                if (migrateFieldMapping.getFieldMappingId() != null) {
                    sql = sql + " AND field_mapping_id =  '" + migrateFieldMapping.getFieldMappingId() + "'";
                }
                if (migrateFieldMapping.getTableMappingId() != null) {
                    sql = sql + " AND table_mapping_id =  '" + migrateFieldMapping.getTableMappingId() + "'";
                }
                if (migrateFieldMapping.getSourceFieldCode() != null) {
                    sql = sql + " AND source_field_code =  '" + migrateFieldMapping.getSourceFieldCode() + "'";
                }
                if (migrateFieldMapping.getSourceFieldName() != null) {
                    sql = sql + " AND source_field_name =  '" + migrateFieldMapping.getSourceFieldName() + "'";
                }
                if (migrateFieldMapping.getFieldMappingRule() != null) {
                    sql = sql + " AND field_mapping_rule =  '" + migrateFieldMapping.getFieldMappingRule() + "'";
                }
                if (migrateFieldMapping.getTargetFieldCode() != null) {
                    sql = sql + " AND target_field_code =  '" + migrateFieldMapping.getTargetFieldCode() + "'";
                }
                if (migrateFieldMapping.getTargetFieldName() != null) {
                    sql = sql + " AND target_field_name =  '" + migrateFieldMapping.getTargetFieldName() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateFieldMapping obj = new MigrateFieldMapping();
				obj.setFieldMappingId(rs.getString("field_mapping_id"));
				obj.setTableMappingId(rs.getString("table_mapping_id"));
				obj.setSourceFieldCode(rs.getString("source_field_code"));
				obj.setSourceFieldName(rs.getString("source_field_name"));
				obj.setFieldMappingRule(rs.getString("field_mapping_rule"));
				obj.setTargetFieldCode(rs.getString("target_field_code"));
				obj.setTargetFieldName(rs.getString("target_field_name"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateFieldMapping is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
